/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


$(document).ready(function () {
    register.init();
    login.init();
});

var register = (function () {

    function initNormalRegister() {
        $('#regSubmit').click(function () {
            $('.form-group').removeClass('has-error');
            
            var name = $('#regName').val(),
                mail = $('#regEmail').val(),
                pass = $('#regPass').val(),
                repass = $('#regRePass').val();
                
            if(!name || name.length > 100) {
                $('#regName').parents('.form-group').addClass('has-error');
                return false;
            }
            if (!mail || !validateEmail(mail)) {
                $('#regEmail').parents('.form-group').addClass('has-error');
                return false;
            }
            if (!pass || pass.length < 6) {
                $('#regPass').parents('.form-group').addClass('has-error');
                return false;
            }
            if (repass !== pass) {
                $('#regRePass').parents('.form-group').addClass('has-error');
                return false;
            }

            $.post('/dang-ki/ajax-register', {
                name: name,
                mail: mail,
                pass: pass
            }, function (resp) {
                var err = resp.err,
                    msg = resp.msg,
                    cls = 'form-control-static text-danger';
                $('.form-group').removeClass('has-error');
                switch (err) {
                    case -2:
                        $('#regEmail').parents('.form-group').addClass('has-error');
                        break;
                    case -3:
                        $('#regPass').parents('.form-group').addClass('has-error');
                        break;
                    case -4:
                        $('#regName').parents('.form-group').addClass('has-error');
                        break;
                    case 0:
                        cls = 'form-control-static text-success';
                        window.location.href = '/';
                        break;
                }
                $('#respMsg').show().find('p').attr('class', cls).html(msg);
            }, 'json');

            return false;
        });
    }
    
    function initFbRegister() {
    
        $('#fbReg').click(function() {
            if(!FB) {
                return false;
            }
            FB.login(function(fbresp) {
                submitRegister('fb', fbresp.authResponse.accessToken);
            }, {scope: 'public_profile, email'});
            return false;
        });
    }
    
    function initGgRegister() {
        $('#ggReg').click(function() {
            var url = 'https://accounts.google.com/o/oauth2/auth?scope=email%20profile&state=%2Fprofile&redirect_uri=http://nmp-v1.tk/oauth2callback&response_type=token&client_id=813790173249-jfumg1bn8krku1sjf2j5v6q1ksjcfb1p.apps.googleusercontent.com';
            window.open(url, 'nmpggreg','height=420,width=500');
            return false;
        });
    }
    
    function submitRegister(type, accessToken) {
        $('#regLoading').show();
        $.post('/dang-ki/ajax-register-' + type, {
            accessToken: accessToken
        }, function(resp) {
            var cls = 'form-control-static text-danger';
            if(resp.err == 0) {
                cls = 'form-control-static text-success';
                window.location.href = '/';
            }
            $('#regLoading').hide();
            $('#respMsg').show().find('p').attr('class', cls).html(resp.msg);
        }, 'json');
    }

    return {
        init: function () {
            initNormalRegister();
            initFbRegister();
            initGgRegister();
        },
        
        ggOauthCb: function (qrStr) {
            if(!qrStr)
                return;
            qrStr = qrStr.substring(1);
            var m, 
                params = {}, 
                regex = /([^&=]+)=([^&]*)/g;
            while (m = regex.exec(qrStr)) {
                params[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
            }
            params.access_token && submitRegister('gg', params.access_token);
        }
    };
})();

var login = (function() {
    
    function initLogin() {
        $('#loginBtn').click(function() {
            var u = $('#loginEmail').val(),
                p = $('#loginPass').val();
                
            $('#loginForm .has-error').removeClass('has-error');
            if (!u || !validateEmail(u)) {
                $('#loginEmail').parents('.form-group').addClass('has-error');
            } else if(!p){
                $('#loginPass').parents('.form-group').addClass('has-error');
            } else {
                $.post('/ajax-do-login', {u: u, p: p}, function(resp) {
                    if (resp.err == 0) {
                        $('#loginForm .has-error').removeClass('has-error');
                        window.location.reload();
                    } else {
                        $('#loginEmail').parents('.form-group').addClass('has-error');
                        $('#loginPass').parents('.form-group').addClass('has-error');
                    }
                }, 'json');
            } 
            return false;
        });
    }
    
    return {
        init: function() {
            initLogin();
        }
    };
})();